package com.jyb.qiyefuwu.viewmodel;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.jyb.qiyefuwu.api.EnterpriseServiceAPIManager;
import com.jyb.qiyefuwu.entity.IpPickSelectEntity;
import com.jyb.qiyefuwu.entity.ServiceListEntiy;
import com.jyb.qiyefuwu.entity.ServiceSecondClassEntiy;
import com.sgb.net.entity.BaseEntity;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.view.entity.MenuEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ServiceTypeModel extends BaseViewModel {

    private List<MenuEntity> listdata = new ArrayList<>();
    public MutableLiveData<String> VerificationCode = new MutableLiveData<>();
    public MutableLiveData<String> publishDemand = new MutableLiveData<>();
    public MutableLiveData<List<MenuEntity>> secondLevelData = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public List<String> checkData = new ArrayList<>();
    public MutableLiveData<String> show_toast = new MutableLiveData<>();
    public String mitemid;
    public String titlename = "";
    public MutableLiveData<List<ServiceListEntiy>> list_data = new MutableLiveData<>();
    private Map map = new HashMap();

    public String companyId = "";

    public ServiceTypeModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void setMitemid(String id) {
        this.mitemid = id;
    }


    /**
     * 二级分类
     */

    public void getServerTitleLable() {
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getEnterPriseNewAPI().getservicecategorySecond(mitemid).enqueue(new Callback<BaseEntity<List<ServiceSecondClassEntiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ServiceSecondClassEntiy>>> call, Response<BaseEntity<List<ServiceSecondClassEntiy>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    listdata.clear();
                    for (ServiceSecondClassEntiy mdata : response.body().getData()) {
                        MenuEntity menuEntity = new MenuEntity();
                        menuEntity.setName(mdata.getService_class());
                        menuEntity.setParentNo(mdata.getParentNo());
                        menuEntity.setLev(mdata.getLev());
                        menuEntity.setId(mdata.getId() + "");
                        //字符太长
                        if (menuEntity.getName().length() > 6) {
                            String name = menuEntity.getName().substring(0, 5) + "...";
                            menuEntity.setName(name);
                        } else {
                            menuEntity.setName(menuEntity.getName());
                        }
                        menuEntity.setAppIcon(mdata.getService_icon());
                        listdata.add(menuEntity);
                    }
                    secondLevelData.postValue(listdata);
                } else {
                    secondLevelData.postValue(null);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ServiceSecondClassEntiy>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    public void setCheckData() {
        checkData.add("公司注册");
        checkData.add("代理记账");
        checkData.add("资质办理");
        checkData.add("税务筹划");
        checkData.add("法律服务");
        checkData.add("知产交易");
    }


    public List<IpPickSelectEntity> getTypeData(){
        List<IpPickSelectEntity> list = new ArrayList<>();
        IpPickSelectEntity entity = new IpPickSelectEntity();
        entity.setName("商标需求");
        entity.setId("1");
        list.add(entity);
        IpPickSelectEntity entity1 = new IpPickSelectEntity();
        entity1.setName("专利需求");
        entity1.setId("3");
        list.add(entity1);
        IpPickSelectEntity entity2 = new IpPickSelectEntity();
        entity2.setName("版权需求");
        entity2.setId("2");
        list.add(entity2);
        IpPickSelectEntity entity3 = new IpPickSelectEntity();
        entity3.setName("服务需求");
        entity3.setId("4");
        list.add(entity3);
        return list;
    }

    public void getServiceList() {
        ShowLoading.postValue(1);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("serviceClass", mitemid);
        hashMap.put("pageSize", 1);
        hashMap.put("limit", 10);
        hashMap.put("type", 1);
        Log.e("message", new Gson().toJson(hashMap));
        EnterpriseServiceAPIManager.getInstance().getEnterPriseNewAPI().getServiceHallCompanyOrTeamList(hashMap).enqueue(new Callback<BaseEntity<List<ServiceListEntiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ServiceListEntiy>>> call, Response<BaseEntity<List<ServiceListEntiy>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    list_data.postValue(response.body().getData());
                } else {
                    show_toast.postValue(response.body().getMsg());
                    secondLevelData.postValue(null);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ServiceListEntiy>>> call, Throwable t) {
                ShowLoading.postValue(2);
                show_toast.postValue(t.getMessage());
            }
        });

    }

    public void getVerificationCode(String phone) {
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getEnterPriseNewAPI().getVerificationCode(phone).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    if (response.body().getMsg() != null)
                        VerificationCode.postValue(response.body().getMsg());
                } else {
                    VerificationCode.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    /**
     * 企业服务 需求快速发布
     *
     * @param poolName
     * @param contactPhone
     * @param code
     */
    public void publishDemandFun(String poolName,int demandType,String estimate,long timeLimit,String contactName, String contactPhone, String code) {
        map.put("demandContent", poolName);
        map.put("contactPhone", contactPhone);
        map.put("demandType",demandType);
        if(!TextUtils.isEmpty(estimate)&&estimate.endsWith(".")){
            estimate = estimate.substring(0,estimate.length()-1);
        }
        map.put("estimate",estimate);
        map.put("timeLimit",timeLimit);
        map.put("contactName",contactName);
        map.put("code", code);
        map.put("demandPort", "2");
        ShowLoading.postValue(1);
//        NewAPIManager
        EnterpriseServiceAPIManager.getInstance().getEnterPriseNewAPI().publishDemandFun(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                ShowLoading.postValue(2);
                if (response.body() == null )
                    return;
                if (response.body().getState().equals("ok")) {
                    if ((Boolean) response.body().getData()) {
                        publishDemand.postValue("操作成功");
                    }
                } else {
                    publishDemand.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                show_toast.postValue(t.getMessage());
                ShowLoading.postValue(2);
            }
        });
    }

    public void setFollow(String no, int type) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("businessNo", no);
        hashMap.put("type", type);
//        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getEnterPriseNewAPI().setFollowForCompanyAndTeam(hashMap).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                ShowLoading.postValue(2);

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getServiceList();

                } else {
                    show_toast.postValue(response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
//                ShowLoading.postValue(2);
            }
        });
    }

    /**
     * 隐藏电话号码中间4位
     *
     * @param phone
     * @return
     */
    public String settingPhone(String phone) {
        if (!TextUtils.isEmpty(phone)) {
            if (phone.length() == 11) { //设置隐藏手机号码
                String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                return phone_s;
            } else if (phone.length() > 7) {//设置隐藏座机号码
                String phone_s = phone.substring(0, 3) + "****" + phone.substring(7);
                return phone_s;
            }
        }
        return "";
    }
}


